package com.gxa.order.service.impl;

import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.pojo.HotelOrderTenancRefund;
import com.gxa.order.config.custFormatDate;
import com.gxa.order.feign.ServiceOrderApi;
import com.gxa.order.mapper.HotelOrderMapper;
import com.gxa.order.mapper.TenantMapper;
import com.gxa.order.pojo.HotelOrder;
import com.gxa.order.pojo.hotel.HotelOrderTenant;
import com.gxa.order.pojo.hotel.Tenant;
import com.gxa.order.service.HotelOrderService;
import com.gxa.order.service.RefundService;
import com.gxa.order.service.TenantService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class HotelOrderServiceimpl implements HotelOrderService {
    @Autowired(required = false)
    private HotelOrderMapper hotelOrderMapper;
    @Autowired
    private RefundService refundService;
    @Autowired
    private TenantService tenantService;
    JsonResult jsonResult = new JsonResult();
    R r = new R();

    /**
     * 查询住宿订单接口
     * @param map
     * @return
     */
    @Override
    public JsonResult findHotelOrderList(Map map) {
        try {
            Integer currentPage = Integer.parseInt(map.get("currentPage").toString());
            Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
            Integer current = (currentPage-1)*pageSize;
            map.put("currentPage",current);
            map.put("pageSize",pageSize);

            List<HotelOrder> hotelOders = hotelOrderMapper.findHotelOderList(map);
            int i = hotelOrderMapper.countHotelOrder(map);
//// 待支付超时（30分钟），状态变成过期
//            for (HotelOrder hotelOrder:hotelOders) {
//                Integer id = hotelOrder.getId();
//                Integer statu = hotelOrder.getStatu();
//                String orderNumber = hotelOrder.getOrderNumber();
//                String strNumber  = custFormatDate.customFormatDate("yyyyMMddHHmmss");
//                if((Long.valueOf(orderNumber)+30<Long.valueOf(strNumber)) && statu==1){
//                    hotelOrderMapper.updateHotelOrderStatu(id);
//                }
//                //            调用住客查询接口？
//                List<Tenant> tenants = tenantService.queryTenant(id);
//                map.put("tenants",tenants);
//            }
//            List<HotelOrder> hotelOders1 = hotelOrderMapper.findHotelOderList(map);

//              调用房间查询接口

            jsonResult.setCode(200);
            jsonResult.setCount(i);
            jsonResult.setData(hotelOders);
            jsonResult.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 住宿订单类型数量
     * @param statu
     * @return
     */
    @Override
    public JsonResult countHotelOrderStatu(Integer statu) {
        try {
        int i = hotelOrderMapper.countHotelOrderStatu(statu);
        jsonResult.setCode(200);
        jsonResult.setCount(i);
        jsonResult.setMsg("查询成功");
    }catch (Exception e){
        e.printStackTrace();
        jsonResult.setCode(500);
        jsonResult.setMsg("系统开小差");
    }
        return jsonResult;
    }

    @Resource
    private ServiceOrderApi serviceOrderApi;
    /**
     * 查看住宿订单详情
     * @param id
     * @return
     */
    @Override
    public JsonResult findHotelOrder(Integer id) {
        Map may = new HashMap();
      try {

            HotelOrderTenancRefund hotelOrderTenancRefund=new HotelOrderTenancRefund();
            HotelOrder hotelOrder = hotelOrderMapper.findHotelOrder(id);

            String orderNumber = hotelOrder.getOrderNumber();
//            调用住客查询接口？
            may.put("hotelOrder",hotelOrder);
//            BeanUtils.copyProperties(hotelOrder,hotelOrderTenancRefund);

//              调用房间查询接口

//            调用住宿接口
//            R hotelById = serviceOrderApi.findHotelById(hotelId);
//            Object data = hotelById.getData();
                //            调用住客查询接口？
                List<Tenant> tenants = tenantService.queryTenant(id);
          for (Tenant tenant:tenants) {
              int i=0;
              i++;
              may.put("tenanti",tenant);
          }

//            BeanUtils.copyProperties(tenants,hotelOrderTenancRefund);
            if (hotelOrder.getStatu()==4||hotelOrder.getStatu()==5||hotelOrder.getStatu()==6||hotelOrder.getStatu()==7){
                System.out.println("退款中");
//                调用退款详情接口
                R findorderid = refundService.findorderid(orderNumber);
                Object data = findorderid.getData();
//                BeanUtils.copyProperties(data,hotelOrderTenancRefund);
                may.put("refund",data);
            }

          jsonResult.setCode(200);
          jsonResult.setData(may);
          jsonResult.setMsg("查询成功");
      }catch (Exception e){
          e.printStackTrace();
          jsonResult.setCode(500);
          jsonResult.setMsg("系统开小差");
      }
        return jsonResult;
    }

    /**
     * 删除住宿订单
     * @param id
     * @return
     */
    @Override
    public R delHotelOrder(Integer id) {
        try {
            int i = hotelOrderMapper.delHotelOrder(id);
            r.setCode(200);
            r.setMsg("删除成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 修改支付状态（支付时间和支付方式）
     * @param payTime
     * @param payType
     * @param statu
     * @param id
     * @return
     */
    @Override
    public R updateHotelOrder(String payTime, Integer payType, Integer statu,Integer id) {
        try {
            int i = hotelOrderMapper.updateHotelOrder(payTime,payType,statu,id);
            r.setCode(200);
            r.setMsg("修改成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 增加住宿订单
     * @param hotelOrderTenant
     * @return
     */
    @Override
    public R addHotelOrder(HotelOrderTenant hotelOrderTenant) {
        try {
            HotelOrder hotelOrder=new HotelOrder();
            Tenant tenant = new Tenant();
            String strNumber  = custFormatDate.customFormatDate("yyyyMMddHHmmss");
            String s = custFormatDate.customFormatDate("yyyy-MM-dd'T'HH:mm:ss");
//  生成订单号
            BeanUtils.copyProperties(hotelOrderTenant,hotelOrder);
            BeanUtils.copyProperties(hotelOrderTenant,tenant);

            hotelOrder.setOrderNumber(strNumber);

//            生成订单时间
            hotelOrder.setOrderTime(s);
            hotelOrder.setStatu(1);
//           取出订单id和房间号
            int i = hotelOrderMapper.addHotelOrder(hotelOrder);
            String orderNumber = hotelOrder.getOrderNumber();
            int id = hotelOrderMapper.findOrderid(orderNumber);

//            插入到住客体中
            tenant.setHotelOrderId(id);
//            tenant.setRoomNo(Integer.parseInt(roomNumber));

            R r1 = tenantService.addTenant(tenant);


//            Map may = new HashMap();
//            may.put("订单id",id);
//            may.put("订单编号",orderNumber);
            this.r.setCode(200);
            this.r.setMsg("添加成功");
            this.r.setData(id);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 修改住宿订单状态
     * @param statu
     * @param id
     * @return
     */
    @Override
    public R updateHotelOrderstatu(Integer statu,Integer id) {
        try {
            int i = hotelOrderMapper.updateOrderStatu(statu,id);
            r.setCode(200);
            r.setMsg("修改成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 修改订单房间号
     * @param id
     * @param room_number
     * @return
     */
    @Override
    public R updateHotelOrderRoom(Integer id,Integer roomId, String room_number) {
        try {
            int i = hotelOrderMapper.updateOrderroom(id,roomId,room_number);
            r.setCode(200);
            r.setMsg("修改成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 根据订单编号查看订单id
     * @param orderNumber
     * @return
     */
    @Override
    public R findOrderid(String orderNumber) {
        try {
            int i = hotelOrderMapper.findOrderid(orderNumber);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 根据订单id查看状态
     * @param id
     * @return
     */
    @Override
    public R findOrderStatu(Integer id) {
        try {
            int i = hotelOrderMapper.findOrderStatu(id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(i);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }


}
